/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-09-29
 * Time: 19:54
 */
public class kth_Smallest {
    static int count = 0;

    public static int kthSmallest(TreeNode root, int k) {
        count = k;
        return dfs(root);

    }

    static int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = dfs(root.left);
        if (left != 0) return left;
        count--;
        if (count == 0) {
            return root.val;
        }
        int right = dfs(root.right);
        if (right != 0) return right;

        return 0;
    }
}
